use strict;
use utf8;

my $dir = "d";
mkdir $dir if (! -d $dir);

my @kw = ("футбол", "хоккей", "баскетбол", "воллейбол", "пенальти", "гол", "матч",
          "чемпионат", "тренер", "форвард", "голкипер", 
          "футбольн", "хоккейн", "баскетбольн", "воллейбольн",
          "болельщик", "УЕФА", "ЦСКА", "Зенит", "Локомотив", "Спартак", "Торпедо", "Динамо",
          "Ювентус", "Реал", "Металлург", "Челси", "Рубин", "Ротор", "Амкар");

my @url_kw = ("spartak", "cska", "lokomotiv", "football", "zenit", "hockey", "semin", "rfs",
              "fanats", "uefacup", "mutko", "fcdynamo", "dynamo", "zenitfc",
              "sytchev", "loko");

my %url_kw_hash = map {$_ => 1} @url_kw;

$/ = "\n\/news\/";

binmode(STDIN, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");

while (<STDIN>) {
  $_ =~ /^(\d+)\/(\d+)\/(\d+)\/([^\/]+)\//;
  my $fn = join("-", ($1, $2, $3, $4));
  my $url_word = $4;

  my $r = is_sport_doc($_);
  print "$fn - $r\n";
  if ($r > 0) {
    open(F, ">$dir\/$r" . "_$fn.txt");
    binmode(F, ":encoding(utf-8)");
    print F $_;
    close(F);
  } elsif (exists $url_kw_hash{$url_word}) {
    open(F, ">$dir\/u$r" . "_$fn.txt");
    binmode(F, ":encoding(utf-8)");
    print F $_;
    close(F);
  }
}

sub is_sport_doc {
  my $text = shift;
  my $r = 0;
  #$text =~ tr/А-Я/а-я/;
  $text = " " . $text;

  while ($text =~ /([А-ЯЁ]+[а-яё]+)/g) {
    my $w = " " . $1;
    foreach my $k (@kw) {
      if ($w =~ / $k/) {
        $r += 1;
      }
    }
  }
  return $r;
}
